package com.ruoyi.workflow.service.impl;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.workflow.domain.CarPackageAudit;
import com.ruoyi.workflow.service.IBpmnNodeService;
import com.ruoyi.workflow.service.ICarPackageAuditService;
import com.ruoyi.workflow.service.ITodoTaskService;
import com.ruoyi.workflow.vo.CarPackageAuditVo;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: renSen
 * @Date: 2022/11/20
 * @Description:
 */
@Service
public class TodoTaskServiceImpl implements ITodoTaskService {
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private ICarPackageAuditService carPackageAuditService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IBpmnNodeService bpmnNodeService;

    @Override
    public TableDataInfo selectForList(CarPackageAudit carPackageAudit) {
        //1.创建返回分页数据对象
        TableDataInfo info = new TableDataInfo();
        info.setCode(HttpStatus.SUCCESS);
        info.setMsg("查询成功");
        //2.获取当前登录用户名
        String username = SecurityUtils.getUsername();
        //3.根据当前登录用户名 代办任务数量
        long total = taskService.createTaskQuery()
                .taskCandidateOrAssigned(username)
                .count();
        info.setTotal(total);
        //4.如果代办任务数量为0 返回空数据
        if (total == 0) {
            info.setRows(Collections.emptyList());
            return info;
        }
        //5.获取分页参数
        PageDomain pageDomain = TableSupport.buildPageRequest();
        int startIndex = (pageDomain.getPageNum() - 1) * pageDomain.getPageSize();
        //6.查询当前登录用户代办任务(分页)
        List<Task> tasks = taskService.createTaskQuery()
                .taskCandidateOrAssigned(username)
                .orderByTaskCreateTime()
                .asc()
                .listPage(startIndex, pageDomain.getPageNum());
        List<CarPackageAudit> list = new ArrayList<>();
        for (Task task : tasks) {
            //7.根据实例ID查询实例对象
            ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
                    .processInstanceId(task.getProcessInstanceId())
                    .singleResult();
            //8.获取业务id
            String businessKey = processInstance.getBusinessKey();
            //9.根据业务id获取审核记录
            CarPackageAudit packageAudit = carPackageAuditService.getById(Long.parseLong(businessKey));
            CarPackageAuditVo vo = new CarPackageAuditVo();
            BeanUtils.copyBeanProp(vo, packageAudit);
            //根据创建人id查询创建人
            SysUser sysUser = userService.selectUserById(Long.parseLong(vo.getCreatorId()));
            //设置创建人
            vo.setCreatorName(sysUser.getNickName());
            //设置当前任务id/名 当前任务审核人
            vo.setTaskName(task.getName());
            vo.setTaskId(task.getId());
            List<SysUser> userList = bpmnNodeService.selectUsersByNodeKey(task.getTaskDefinitionKey());
            List<String> collect = userList.stream().map(SysUser::getNickName).collect(Collectors.toList());
            vo.setAuditors(JSON.toJSONString(collect));
            list.add(vo);
        }
        info.setRows(list);
        return info;
    }
}
